Ext.define('BISC.view.FilterManagerPanel', {
	extend: 'Ext.panel.Panel',
	alias: ['widget.filtermanagerpanel'],
	requires: [
        'BISC.view.FormCreateFilter',
        'BISC.view.CtxMnuFilter'
    ],
	id: 'filterManagerPanel',
	layout: 'fit',
	border: false,
	bodyBorder: false,
	initComponent: function() {
		var me = this;
		Ext.applyIf(me, {
			items: [{
				xtype: 'tabpanel',
				id: 'filterManagerTabPanel',
				activeTab: 0,
                border: false,
                layout: 'fit',
				items: [{
					xtype: 'gridpanel',
					iconCls: 'icon_filters',
					title: 'Automatic Attribution',
					id: 'filtersGrid',
					border: false,
					store: 'FiltersStore',
					columns: [
                        {
						    dataIndex: 'name',
						    text: 'Name',
                            flex: 2
					    },
                        {
                            xtype: 'booleancolumn',
                            dataIndex: 'enabled',
                            text: 'Enabled?',
                            flex: 1,
                            falseText: 'No',
                            trueText: 'Yes',
                            undefinedText: 'n/a'
					    },
                        {
                            dataIndex: 'data',
                            text: 'Filter Object',
                            flex: 6
                        }
                    ],
                    scope: this,
					listeners: {
						itemdblclick: function( grid, record, el, ind, e, opts ) {
                            var me = this;
                            var form = Ext.create( 'BISC.view.FormCreateFilter', { filter: record } );
							var wndw = Ext.create('Ext.window.Window', {
								title: 'Edit Filter',
								iconCls: 'icon_editFilter',
								modal: true,
								height: 500,
								width: 600,
								layout: 'fit',
                                resizable: false,
                                autoScroll: true,
								items: [
                                    form
                                ]
							}).show();
                            wndw.on( 'done', function() {
                                wndw.close();
                                Ext.getCmp('filtersGrid').getStore().load();
                            });
						},
						itemcontextmenu: function(view, record, item, index, e) {
							e.stopEvent();
							Ext.create('BISC.view.CtxMnuFilter', {record: record}).showAt( e.getXY() );
						}
					},
					dockedItems: [{
						xtype: 'toolbar',
						dock: 'top',
						items: [{
							text: 'Add Filter',
							iconCls: 'icon_addFilter',
							scope: this,
							handler: this.createFilter
						}]
					}]
				}]
			}]
		});

		me.callParent(arguments);
	},
	createFilter: function() {
        var me = this;
        var form = Ext.create( 'BISC.view.FormCreateFilter', {
            filter: {
                name: '',
                enabled: false,
                tempId: 0,
                monitors: [],
                filters: [],
                attributes: []
            }
        });
		var tmpwndw = Ext.create('Ext.window.Window', {
			title: 'Add New Filter',
			iconCls: 'icon_addFilter',
			modal: true,
			height: 500,
			width: 600,
			layout: 'fit',
            resizable: false,
            autoScroll: true,
			items: [
                form
			]
		}).show();
        tmpwndw.on( 'done', function() {
            tmpwndw.close();
            Ext.getCmp('filtersGrid').getStore().load();
        });
	}

});
